/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.CI.Profile;

import entity.AES;
import entity.Employee;
import java.awt.event.ActionEvent;
import javax.ejb.EJB;
import javax.el.ELContext;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import managedbean.Controller;
import session.stateless.EmployeeBeanLocal;

/**
 *
 * @author Vincent
 */
@ManagedBean
@ViewScoped
public class changePasswordManagerBean {
    @EJB
    private EmployeeBeanLocal employeeBeanLocal;
    
    private String currentPassword;
    private String newPassword;
    private String confirmPassword;
    private Employee employeeToEdit;
    private String statusMessage;
    
    /** Creates a new instance of changePasswordManagerBean */
    public changePasswordManagerBean() {
    }

    public String getCurrentPassword() {
        return currentPassword;
    }

    public void setCurrentPassword(String currentPassword) {
        this.currentPassword = currentPassword;
    }

    public String getConfirmPassword() {
        return confirmPassword;
    }

    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }

    public String getNewPassword() {
        return newPassword;
    }

    public void setNewPassword(String newPassword) {
        this.newPassword = newPassword;
    }

    public Employee getEmployeeToEdit() {
        return employeeToEdit;
    }

    public void setEmployeeToEdit(Employee employeeToEdit) {
        this.employeeToEdit = employeeToEdit;
    }
        
    public void initView(PhaseEvent event){
        ELContext elContext = FacesContext.getCurrentInstance().getELContext();
        Controller controller = (Controller) FacesContext.getCurrentInstance().getApplication().getELResolver().getValue(elContext, null, "controller");
        this.employeeToEdit = controller.getEmployee();
    }
    
    public void validateCorrectPassword(FacesContext context, UIComponent toValidate, Object value){
        
        String enteredPassword = (String) value;
        String decryptedPassword = "";
        try{
            AES aes = new AES();
            decryptedPassword = aes.decrypt(employeeToEdit.getPassword());
        }
        catch (Exception e){
            
        }
        if (!decryptedPassword.equals(enteredPassword)) {
            ((UIInput)toValidate).setValid(false);
            FacesMessage message = new FacesMessage("Incorrect Password. Please re-enter.");
            message.setSeverity(FacesMessage.SEVERITY_WARN);
            context.addMessage(toValidate.getClientId(context), message);
        }
    }

    public void validateSamePassword(FacesContext context, UIComponent toValidate, Object value){
        
        String enteredPassword = (String) value;
        String newPassword = (String) toValidate.getAttributes().get("newPassword");
        
        if (!enteredPassword.equals(newPassword)) {
            ((UIInput)toValidate).setValid(false);
            FacesMessage message = new FacesMessage("Password does not match. Please re-enter.");
            message.setSeverity(FacesMessage.SEVERITY_WARN);
            context.addMessage(toValidate.getClientId(context), message);
        }
    }
    
    public void changePassword(ActionEvent event, UIComponent component){
        System.out.println("Confirm Password: " + this.getConfirmPassword() + " New Password: " + this.getNewPassword());
        statusMessage = "Successful!";
        employeeBeanLocal.updatePassword(employeeToEdit.getEmployeeId(), this.newPassword);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,  "Update Password Result: " +statusMessage , "")); 
        this.setCurrentPassword("");
        this.setNewPassword("");
        this.setConfirmPassword("");
    }
}
